package main

import (
	"fmt"
	"github.com/jasonlvhit/gocron"
	"github.com/jinzhu/gorm"
	"gration/common"
	"gration/config"
	"gration/gather"
	"gration/model"
	"time"
)

func main() {
	//1、连接数据库
	config.InitConfig()   //读取配置文件
	db := common.Initdb() //数据库初始化
	defer db.Close()      //延迟关闭
	chansmokinginfostwo := make(chan []model.SmokingInfosTwo)
	go gather.SmokePowerOneDay(db,chansmokinginfostwo)
	gocron.Every(3).Seconds().
		Do(scanningdata,db,chansmokinginfostwo)
	//gocron.Every(3).Seconds().Lock().
	//	Do(gather.SmookePop)
	<-gocron.Start()
	gather.TimeDrawing()
	gather.GetNetworkingNumber()
	//gather.SmokePowerOneDay(db,chansmokinginfostwo)
	//Citylymigration()
	//NationalThere()

	//3、定时任务，每天凌晨3点
	/*var wg sync.WaitGroup//使用sync.WaitGroup保证主 goroutine 不退出。
	wg.Add(1)
	c := cron.New() //创建一个定时任务的管理器
	//定时迁移 每天凌晨3点
	c.AddFunc("0 0 3 * * *", Weeklymigration)
	c.Start()//调用管理器的Start()方法启动它
	defer c.Stop()
	wg.Wait() */ //使主线程阻塞在此，不退出

}
func scanningdata(db *gorm.DB,ch chan []model.SmokingInfosTwo ){
      timeNow:=time.Now().Format("2006-01-02")
      timeNowstart := timeNow + " 00:00:00"
      timeNowend := timeNow + " 23:59:59"
      var info []model.SmokingInfosTwo
      db.Table("smoking_infos_two").Where("handle_flag = ? AND smoke_time between ? and ?",0,timeNowstart,timeNowend).Find(&info)

      if len(info) != 0{
      	ch <- info
      	fmt.Println("扫描到的数据：",info,timeNowstart)
	  }else{
		  fmt.Println("没有数据，时间： ",timeNowstart)
	  }
}
